﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace JinYuan.ControlLib
{
    public partial class PanelExt : Panel
    {
        public PanelExt()
        {
            InitializeComponent();

            //设置控件样式，去除缓冲
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        private int topGap;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示上边间隔距离")]
        public int TopGap
        {
            get { return topGap; }
            set
            {
                topGap = value;
                this.Invalidate();//点击后立即生效
            }
        }

        private int bottomGap;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示下边间隔距离")]
        public int BottomGap
        {
            get { return bottomGap; }
            set
            {
                bottomGap = value;
                this.Invalidate();//点击后立即生效
            }
        }

        private int leftGap;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示左边间隔距离")]
        public int LeftGap
        {
            get { return leftGap; }
            set
            {
                leftGap = value;
                this.Invalidate();//点击后立即生效
            }
        }

        private int rightGap;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示右边间隔距离")]
        public int RightGap
        {
            get { return rightGap; }
            set
            {
                rightGap = value;
                this.Invalidate();//点击后立即生效
            }
        }

        private int bordeWidth = 2;
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示边框的宽度")]
        public int BordeWidth
        {
            get { return bordeWidth; }
            set
            {
                bordeWidth = value;
                this.Invalidate();//点击后立即生效
            }
        }

        private Color bordeColor = Color.FromArgb(35, 255, 253);
        [Browsable(true)]//是否可见
        [Category("自定义属性")] //类别
        [Description("设置或显示边框的颜色")]
        public Color BordeColor
        {
            get { return bordeColor; }
            set
            {
                bordeColor = value;
                this.Invalidate();//点击后立即生效
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //准备画布
            Graphics graphics = e.Graphics;
            //准备笔
            Pen pen = new Pen(bordeColor, bordeWidth);
            //准备矩形参数
            float x = leftGap + bordeWidth * 0.5f;
            float y = topGap + bordeWidth * 0.5f;
            float width = this.Width - leftGap - rightGap - bordeWidth;
            float height = this.Height - topGap - bottomGap - bordeWidth;

            //绘制矩形
            graphics.DrawRectangle(pen, x, y, width, height);
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        #endregion
    }
}
